/**
 * 初始化退货出库详情对话框
 */
var GoodsReturnInfoDlg = {
    id:"GoodsTable",
    goodsReturnInfoData: {},
    table:null,
    newRow: {}
};


/**
 * 初始化表格的列
 */
GoodsReturnInfoDlg.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '退货主键', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '实付金额', field: 'amountPaid', visible: true, align: 'center', valign: 'middle'},
        {title: '应付金额', field: 'amountPayable', visible: true, align: 'center', valign: 'middle'},
        {title: '退货单号', field: 'returnOrderNumber', visible: true, align: 'center', valign: 'middle'},
        {title: '是否支付', field: 'payState', visible: true, align: 'center', valign: 'middle'},
        {title: '创建者', field: 'createBy', visible: false, align: 'center', valign: 'middle'},
        {title: '创建时间', field: 'createDate', visible: true, align: 'center', valign: 'middle'},
        {title: '更新者', field: 'updateBy', visible: false, align: 'center', valign: 'middle'},
        {title: '更新时间', field: 'updateDate', visible: false, align: 'center', valign: 'middle'},
        {title: '备注信息', field: 'remarks', visible: true, align: 'center', valign: 'middle'},
        {title: '删除标记（0：正常；1：删除）', field: 'delFlag', visible: true, align: 'center', valign: 'middle'},
        {title: '供应商', field: 'supplierId', visible: true, align: 'center', valign: 'middle'}
    ];
};


/**
 * 清除数据
 */
GoodsReturnInfoDlg.clearData = function () {
    this.goodsReturnInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
GoodsReturnInfoDlg.set = function (key, val) {
    this.goodsReturnInfoData[key] = (typeof val === "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
GoodsReturnInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
GoodsReturnInfoDlg.close = function () {
    parent.layer.close(window.parent.GoodsReturn.layerIndex);
}

/**
 * 收集数据
 */
GoodsReturnInfoDlg.collectData = function () {
    this
        .set('id')
        .set('amountPaid')
        .set('amountPayable')
        .set('returnOrderNumber')
        .set('payState')
        .set('createBy')
        .set('createDate')
        .set('updateBy')
        .set('updateDate')
        .set('remarks')
        .set('delFlag')
        .set('supplierId');
}


/**
 * 初始化商品表格
 */

GoodsReturnInfoDlg.initGoodsColumns = function () {
    return [
        {title: '商品ID', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '商品编码', field: 'goodsCode', visible: true, align: 'center', valign: 'middle'},
        {title: '商品名称', field: 'goodsName', visible: true, align: 'center', valign: 'middle'},
        {title: '商品型号', field: 'goodsModel', visible: true, align: 'center', valign: 'middle'},
        {title: '商品单位', field: 'goodsUnit', visible: true, align: 'center', valign: 'middle'},
        {title: '采购价', field: 'goodsPurchasePrice', visible: true, align: 'center', valign: 'middle'},
        {title: '出售价格', field: 'goodsSellingPrice', visible: true, align: 'center', valign: 'middle'},
        {title: '库存数量', field: 'goodsInventoryQuantity', visible: true, align: 'center', valign: 'middle'},
        {title: '库存下限', field: 'goodsMinNum', visible: false, align: 'center', valign: 'middle'},
        {title: '上次采购价格', field: 'goodsLastPurchasePrice', visible: false, align: 'center', valign: 'middle'},
        {title: '创建者', field: 'createBy', visible: false, align: 'center', valign: 'middle'},
        {title: '创建时间', field: 'createDate', visible: false, align: 'center', valign: 'middle'},
        {title: '更新者', field: 'updateBy', visible: false, align: 'center', valign: 'middle'},
        {title: '更新时间', field: 'updateDate', visible: false, align: 'center', valign: 'middle'},
        {title: '备注信息', field: 'remarks', visible: false, align: 'center', valign: 'middle'},
        {title: '删除标记（0：正常；1：删除）', field: 'delFlag', visible: false, align: 'center', valign: 'middle'},
        {title: '供应商名称', field: 'supplierId', visible: false, align: 'center', valign: 'middle'},
        {title: '商品分类id', field: 'goodsCategoryId', visible: false, align: 'center', valign: 'middle'}
    ];
}

/**
 * 点击商品分类 刷新整个表格，不加右边的查询条件
 * @param e
 * @param treeId
 * @param treeNode
 */
GoodsReturnInfoDlg.onClickCate = function (e, treeId, treeNode) {
    var queryData = {"goodsCategoryId": treeNode.id};
    GoodsReturnInfoDlg.table.refresh({query: queryData});
};

/**
 * 搜索 只搜索输入的条件，忽略树结构
 */
GoodsReturnInfoDlg.search = function () {
    var queryData = {};
    //名字或者编码都放在这个字段里面
    queryData['goodsCode'] = $("#sGoodsCode").val();
    GoodsReturnInfoDlg.table.refresh({query: queryData});
}

/**
 * 清除查询条件
 */
GoodsReturnInfoDlg.clear = function(){
    $("#sGoodsCode").val('');
    $("#sGoodsName").val('');
}

/**
 * 提交添加
 */
GoodsReturnInfoDlg.addSubmit = function () {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/goodsReturn/add", function (data) {
        Feng.success("添加成功!");
        window.parent.GoodsReturn.table.refresh();
        GoodsReturnInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.goodsReturnInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
GoodsReturnInfoDlg.editSubmit = function () {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/goodsReturn/update", function (data) {
        Feng.success("修改成功!");
        window.parent.GoodsReturn.table.refresh();
        GoodsReturnInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.goodsReturnInfoData);
    ajax.start();
}

/**
 * 表格增加行点击事件,打开新窗口
 */
GoodsReturnInfoDlg.addClickRowEvent = function () {
    this.table.btInstance.on('click-row.bs.table', function (e, row, element) {
        //判断是否添加相同的商品
        var rows = window.parent.GoodsReturn.table.btInstance.bootstrapTable('getData');
        var isAdd = false;
        $.each(rows, function (index, pRow) {
            if (row.id === pRow.id) {
                Feng.info('该商品已添加!');
                isAdd = true;
            }
        })

        if (!isAdd) {
            GoodsReturnInfoDlg.newRow['goodsId'] = row.id;
            GoodsReturnInfoDlg.newRow['goodsCode'] = row.goodsCode;
            GoodsReturnInfoDlg.newRow['goodsName'] = row.goodsName;
            GoodsReturnInfoDlg.newRow['goodsModel'] = row.goodsModel;
            GoodsReturnInfoDlg.newRow['goodsUnit'] = row.goodsUnit;
            GoodsReturnInfoDlg.newRow['goodsReturnPrice'] = row.goodsPurchasePrice;
            GoodsReturnInfoDlg.newRow['goodsInventoryQuantity'] = row.goodsInventoryQuantity;

            $("#goodsCode").val(row.goodsCode);
            $("#goodsName").val(row.goodsName);
            $("#goodsModel").val(row.goodsModel);
            $("#goodsUnit").val(row.goodsUnit);
            $("#goodsPurchasePrice").val(row.goodsPurchasePrice);
            $("#goodsInventoryQuantity").val(row.goodsInventoryQuantity);

            //弹出modal框
            $("#goodsDetail").modal();
            // $("#purchaseNumber").focus();
            // setTimeout("document.getElementById(\"purchaseNumber\").focus()",50);
        }
    });
}

/**
 * 增加保存到父UI的table中
 */
GoodsReturnInfoDlg.saveToParentTable = function () {
    $("#saveBtn").on('click', function () {
        //进货数量
        var returnNumbers = $("#returnNumber").val();
        //正整数
        if (!(/(^[1-9]\d*$)/.test(returnNumbers))) {
            Feng.error("请正确填写进货数量!");
            return;
        }
        GoodsReturnInfoDlg.newRow['goodsNum'] = returnNumbers;
        //当前总价属性
        GoodsReturnInfoDlg.newRow['totalPrice'] = (GoodsReturnInfoDlg.newRow['goodsNum'] * GoodsReturnInfoDlg.newRow['goodsReturnPrice']);
        //最终传输的数据
        var data = GoodsReturnInfoDlg.newRow;
        //找到父table添加row
        window.parent.GoodsReturn.table.btInstance.bootstrapTable('append', data);
        //计算并赋值给父UI的input框
        window.parent.GoodsReturn.calculateTotalPrice(GoodsReturnInfoDlg.newRow['totalPrice']);
        //隐藏modal
        $("#goodsDetail").modal('hide');
    })
}


$(function () {

    var ztree = new $ZTree("goodsCateTree", "/goodsCategory/tree");
    ztree.bindOnClick(GoodsReturnInfoDlg.onClickCate);
    ztree.init();

    var defaultColunms = GoodsReturnInfoDlg.initGoodsColumns();
    var table = new BSTable(GoodsReturnInfoDlg.id, "/managerGoods/list", defaultColunms);
    table.setTableHeight(0);
    table.setShowColumns(false);
    table.setShowRefresh(false);
    GoodsReturnInfoDlg.table = table.init();

    //增加表格行点击事件
    GoodsReturnInfoDlg.addClickRowEvent();
    //增加表格保存数据到父table事件
    GoodsReturnInfoDlg.saveToParentTable();

});
